package com.gcb.utils;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gcb.common.constant.CommonConstants;
import com.gcb.common.constant.GcbUserConstants;
import com.gcb.common.core.domain.entity.GcbUser;
import com.gcb.common.core.redis.RedisCache;
import com.gcb.common.enums.AuthStatusEnum;
import com.gcb.common.enums.CreateTypeEnum;
import com.gcb.common.enums.DeleteFlagEnum;
import com.gcb.common.enums.SexTypeEnum;
import com.gcb.common.utils.DateUtils;
import com.gcb.common.utils.PasswordUtils;
import com.gcb.service.IGcbUserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;

@Component
public class GcbUtils {

    @Resource
    private RedisCache redisCache;

    /**
     * 初始化用户信息（APP注册 + 后台创建用户时的公用方法）
     *
     * @param phone    手机号
     * @param type     创建类型
     * @param createBy 创建人id（APP注册是0，后台创建是操作人）
     * @return GcbUser
     */
    public GcbUser initUserInfo(String phone, CreateTypeEnum type, Long createBy) {
        GcbUser gcbUser = new GcbUser();
        gcbUser.setGcbUserCode(checkUserCode());
        //设置用户的默认头像
        gcbUser.setAvatar(GcbUserConstants.GCB_USER_AVATAR_URL);
        gcbUser.setFaceUrl(GcbUserConstants.GCB_USER_AVATAR_URL);
        // 工作经验
        gcbUser.setExperience(0);
        gcbUser.setSex(SexTypeEnum.MAN.getCode());
        // 状态 1正常
        gcbUser.setStatus(CommonConstants.GcbUser.STATE_NORMAL);
        gcbUser.setAuthStatus(AuthStatusEnum.UN_VERIFIED.getCode());
        gcbUser.setDelFlag(DeleteFlagEnum.EXIST.getCode());
        //默认用户名和昵称为"昵称"
        gcbUser.setName(GcbUserConstants.GCB_USER_NAME);
        gcbUser.setNickName(GcbUserConstants.GCB_USER_NAME);
        //设置手机号
        gcbUser.setMobile(phone);
        //默认密码手机号后6位
        gcbUser.setPassword(PasswordUtils.getDefaultAppUserPassword(phone));
        //身份证号码默认""
        gcbUser.setIdentityCard(GcbUserConstants.GCB_USER_IDENTITY_CARD);
        //自己注册
        gcbUser.setSignType(type.getCode());
        //设置创建时间
        gcbUser.setCreateTime(DateUtils.getNowDate());
        gcbUser.setCreateBy(createBy);
        gcbUser.setUpdateTime(DateUtils.getNowDate());
        gcbUser.setUpdateBy(createBy);
        return gcbUser;
    }

    private Long checkUserCode() {
        Long userCode = redisCache.getCacheObject(CommonConstants.Redis.USER_CODE_PREFIX);
        if (ObjUtil.isNull(userCode)) {
            String userCodeValue = new SimpleDateFormat("yyMMdd").format(DateUtils.getNowDate()) + "0001";
            redisCache.setCacheObject(CommonConstants.Redis.USER_CODE_PREFIX,
                    userCodeValue,
                    Math.toIntExact(CommonConstants.Redis.USER_CODE_EXPIRE),
                    TimeUnit.SECONDS);
            return Long.valueOf(userCodeValue);
        } else {
            redisCache.setCacheObject(CommonConstants.Redis.USER_CODE_PREFIX,
                    userCode + 1,
                    Math.toIntExact(CommonConstants.Redis.USER_CODE_EXPIRE),
                    TimeUnit.SECONDS);
            return userCode;
        }
    }
}
